/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plagiadetector.driver;
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import plagiadetector.datatype.PANCorpus;
import plagiadetector.utility.FileUtils;
import plagiadetector.utility.PANXMLParser;
/**
 *
 * @author Dell
 */
public class PANXMLParserDriver {
  
    //we will use this class to experiment with the corpus too.

    public static ArrayList<PANCorpus> globalCorpus;
    
    public static void initiate(){
        globalCorpus = new ArrayList<PANCorpus>();
    }
    
    /**
     * Get all PAN Corpus XML from a file or directory.
     * @param addr file or directory address.
     */
    public static void getAllXML(String addr){
        initiate();
        File f = new File(addr);
        
        if(f.isFile()){
            processSingleXML(addr);
        }else{
            processDirectory(addr);
        }
        
        System.out.println("Number of processed XML(s) : "+globalCorpus.size());
    }
    
    public static void processSingleXML(String addr){
        globalCorpus.add(PANXMLParser.parseXML(addr));
    }
     
    public static void processDirectory(String addr){
        ArrayList<String> xmls = FileUtils.getFileNames(addr, "*.xml");        
        ArrayList<String> dirs = FileUtils.getSubDirectories(addr);
        
        for(String s : xmls){
            processSingleXML(addr+"\\"+s);
        }
       for(String s : dirs){
           processDirectory(addr+"\\"+s);
       }
    }
    
    public static void main(String[] args) throws FileNotFoundException, IOException{
        
        String corpusaddr;
//        corpusaddr = "D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\Korpus\\PAN-PC-2011\\intrinsic-detection-corpus\\suspicious-document";
        corpusaddr = "D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\Korpus\\Zakiy\\corpus\\PLAGIAT BARU";
        PANXMLParserDriver.getAllXML(corpusaddr);
      
        for(PANCorpus pc : PANXMLParserDriver.globalCorpus){
            System.out.println("File name : "+pc.address+" | Doc length : "+pc.getCorpusText().length()+" | Number of Cases : "+pc.getNumPANCase());
        }
         
/*        
          String xmladdr = "corpusexamples\\PAN2011\\suspicious-document00001.xml";
          PANCorpus panc;
          //initiate PANXMLParser
          panc = PANXMLParser.parseXML(xmladdr);
          panc.printCorpusInfo();
          
          
          String corpusaddr = "D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\Korpus\\PAN-PC-2011\\intrinsic-detection-corpus\\suspicious-document\\part1";
          ArrayList<String> XMLs = FileUtils.getFileNames(corpusaddr, "*.xml");
          ArrayList<PANCorpus> PANCorps = new ArrayList<PANCorpus>();
          
          for(int i=0; i<XMLs.size(); i++){
              PANCorps.add(PANXMLParser.parseXML(corpusaddr+"\\"+XMLs.get(i)));              
          }
          System.out.println("Total files :"+PANCorps.size()+" file(s)");

          System.out.println("---------------Corpus Metadata--------------------");
          for(int i=0; i<XMLs.size(); i++){
              PANCorps.get(i).printCorpusInfo();              
          }

      */
        
         /* 
          System.out.println("---------------Corpus Text Data--------------------");
          ArrayList<String> ark;
          for(int i=0; i<XMLs.size(); i++){
               ark = PANCorps.get(i).getCorpusTextArrayList();
               System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");                   
               //System.out.println("Panjang teks ke "+i+": ");

               for(int j=0; j<ark.size();j++){
                   System.out.println(ark.get(j));
               }
                
               
          }
          
         */ 
        
        
       
        /*
        //checking whetehr a marker is valid to use.
        String marker = "xxyyzz";
        int i = 0;
        boolean found = false;
        
        while(i < PANXMLParserDriver.globalCorpus.size() && !found){
            if(PANXMLParserDriver.globalCorpus.get(i).getCorpusText().contains(marker)){
                found = true;
                System.out.println("found on corpus number "+i+".");
            } 
            else {
                System.out.println("not found on corpus number "+i+".");
                i++;
            }
        }
        
        System.out.println("Is marker \""+marker+"\" appears inside the corpus : "+found);
    */
       
    /*
        int totalcase = 0;
        for(PANCorpus pc : PANXMLParserDriver.globalCorpus){
            totalcase += pc.plagCases.size();
        }
        
        System.out.println("Total real case on the corpus : "+totalcase);
    */
    
       
        //COPYING FOR CORPUS DIVISION
 /*       
        int[] lengththreshold = new int[3];
        lengththreshold[0] = 346128; //lower bound long
        lengththreshold[1] = 41831; //lower bound medium
        lengththreshold[2] = 0; //lower bound short
        int type;
        int textlength;
        
        String targetfolder = "D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\Korpus\\PAN-PC-2011-Divided\\length\\three";
        
        File f1;
        File f2;
        
        Path p1;
        Path p2;
        
        for(PANCorpus pc : PANXMLParserDriver.globalCorpus){
            textlength = pc.getCorpusText().length();
            
            if(textlength > lengththreshold[0]){
                type = 0;
            }else if (textlength > lengththreshold[1]){
                type = 1;
            }else{
                type = 2;
            }
            
            //copy the xml file
            f1 = new File(pc.address);
            f2 = new File(targetfolder+"\\"+type+"\\"+f1.getName());
            p1 = FileSystems.getDefault().getPath(f1.getParent(), f1.getName());
            p2 = FileSystems.getDefault().getPath(f2.getParent(), f2.getName());
            
            Files.copy(p1,p2,StandardCopyOption.REPLACE_EXISTING);
            
            //copy the txt file
            f1 = new File(pc.refaddress);
            f2 = new File(targetfolder+"\\"+type+"\\"+f1.getName());
            p1 = FileSystems.getDefault().getPath(f1.getParent(), f1.getName());
            p2 = FileSystems.getDefault().getPath(f2.getParent(), f2.getName());        
            Files.copy(p1,p2,StandardCopyOption.REPLACE_EXISTING);
            
        }
 */     
        
        //CHECKING PLAGIARISM TYPES.
/*
        ArrayList<String> plagtypes = new ArrayList<String>();
        ArrayList<Boolean> obfuscationtypes = new ArrayList<Boolean>();
        int pancases;
                
        for(PANCorpus pc : globalCorpus){
            pancases = pc.getNumPANCase();
            for(int i = 0; i < pancases; i++){
                if(!plagtypes.contains(pc.getPANCase(i).type)){
                    plagtypes.add(pc.getPANCase(i).type);
                }
            }
        }
        
                        
        for(PANCorpus pc : globalCorpus){
            pancases = pc.getNumPANCase();
            for(int i = 0; i < pancases; i++){
                if(!obfuscationtypes.contains(pc.getPANCase(i).obfuscated)){
                    obfuscationtypes.add(pc.getPANCase(i).obfuscated);
                }
            }
        }
        
        System.out.println("Type of plagiarism in this corpus : ");
        for(String s : plagtypes){
            System.out.println(s);
        }
        
       System.out.println("Obfuscation type of plagiarism in this corpus : ");
        for(Boolean b : obfuscationtypes){
            System.out.println(b);
        }
 */
    }
}
